﻿/******************************************************************
** 文 件 名: Bu_AuditBatch_Edit.aspx.cs
** Copyright (c) 2010-2012 Honestysoft (GuangZhou) Co.,LTD
** 创 建 人: 此代码由生成器自动生成
** 创建日期: 2012-3-21 15:53:10
** 描    述: 资金申请批次编辑页面
** 版    本: 2.0.0.0
** 修改描述:
** 修 改 人:
** 修改日期:
******************************************************************/
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Honestysoft.BaseLibrary;
using Honestysoft.BaseObject;
using Honestysoft.BaseLibrary.ExceptionHandling;
using Honestysoft.Baseframework.WebUI;
using Honestysoft.Financing.Model;
using System.Collections.Generic;


namespace Honestysoft.Financing.WebUI.Module.Audit
{
    public partial class Bu_AuditBatch_Edit : BasePage
    {
        protected override void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                BindControl();
                Bind();
            }
        }

        private void BindControl()
        {
            ControlHelper.BindEnum(ddlBatchStatus, "BatchStatus");//此处最好改成typeof(EnumItem.XXX)
        }

        private void Bind()
        {
            Bu_AuditBatch obj = null;
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj = new BaseBusinessLogic<Bu_AuditBatch>().GetEntity(Request.QueryString["ID"]);
                hidID.Value = obj.ID;
                //txtID.Text = obj.ID;
                numBatchNo.Text = obj.BatchNo.ToString();
                dtpPStartDate.Text = (obj.PStartDate.HasValue ? obj.PStartDate.Value.ToString("yyyy-MM-dd") : "");
                dtpPEndDate.Text = (obj.PEndDate.HasValue ? obj.PEndDate.Value.ToString("yyyy-MM-dd") : "");
                dtpStartDate.Text = (obj.StartDate.HasValue ? obj.StartDate.Value.ToString("yyyy-MM-dd") : "");
                dtpEndDate.Text = (obj.EndDate.HasValue ? obj.EndDate.Value.ToString("yyyy-MM-dd") : "");
                txtRemark.Text = obj.Remark;
                txtCreator.Text = new BaseBusinessLogic<Honestysoft.Baseframework.Model.Sys_User>(Globals.CurrentConnStringName).GetEntity(obj.Creator).UserName;
                hidCreator.Value = obj.Creator;
                //txtCloseUser.Text = obj.CloseUser;
                dtpCreateTime.Text = obj.CreateTime.ToString("yyyy-MM-dd HH:mm:ss");
                ddlBatchStatus.SelectedValue = obj.BatchStatus;
                QueryParameters query = new QueryParameters();
                query.Add("BatchID", obj.ID, QueryParameters.OperatorType.Equal);
                List<Bu_BatchContract> lsContract = new BaseBusinessLogic<Bu_BatchContract>(Globals.CurrentConnStringName).GetEntitys(query);
                List<Bu_BatchRec> lsRec = new BaseBusinessLogic<Bu_BatchRec>(Globals.CurrentConnStringName).GetEntitys(query);
                if (lsContract != null && lsContract.Count > 0)
                {
                    foreach (Bu_BatchContract item in lsContract)
                    {
                        if (hidContractIDString.Value != "") { hidContractIDString.Value += ","; }
                        hidContractIDString.Value += item.ContractID;
                    }
                    BindContract();
                }

                if (lsRec != null && lsRec.Count > 0)
                {
                    foreach (Bu_BatchRec item in lsRec)
                    {
                        if (hidRecIDString.Value != "") { hidRecIDString.Value += ","; }
                        hidRecIDString.Value += item.RecID;
                    }
                }
            }
            else
            {
                hidID.Value = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                numBatchNo.Text = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetNextBatchNo().ToString();
                dtpPStartDate.Text = DateTime.Today.ToString("yyyy-MM-dd");
                dtpPEndDate.Text = DateTime.Today.AddDays(2).ToString("yyyy-MM-dd");

                dtpStartDate.Text = dtpPStartDate.Text;
                dtpEndDate.Text = dtpPEndDate.Text;

                hidCreator.Value = UserLoginer.LoginUser.ID;
                txtCreator.Text = UserLoginer.LoginUser.Name;
                dtpCreateTime.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                ddlBatchStatus.SelectedValue = ((int)EnumItem.BatchStatus.Active).ToString();
            }

        }

        private void BindContract()
        {
            ////if (hidContractIDString.Value.Trim().Length == 0) { return; }
            //string[] arrId = null;
            //if (hidContractIDString.Value.Trim().Length > 0)
            //{
            //    arrId = hidContractIDString.Value.Split(',');
            //}
            //DataTable dt = new Honestysoft.Financing.BusinessLogic.Contract(Globals.CurrentConnStringName).GetContractProject(arrId);
            //rptBatchContract.DataSource = dt;
            //rptBatchContract.DataBind();
            string[] arrID = null;
            if (hidRecIDString.Value.Trim().Length > 0)
            {
                arrID = hidRecIDString.Value.Trim().Split(',');
                DataTable dt = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).GetDocReceives(arrID);
                rptBatchContract.DataSource = dt;
                rptBatchContract.DataBind();
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                Edit();
            }
            else
            {
                Add();
            }
        }

        /// <summary>
        /// 新增
        /// </summary>
        private void Add()
        {
            //try
            //{
            //    Bu_AuditBatch obj = GetSubmitObject();
            //    bool ret = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).CreateBatch(obj);
            //    if (ret)
            //    {
            //        ScriptHelper.Window.Alert("保存成功！", "window.location.href='Bu_AuditBatch_View.aspx?ID=" + obj.ID + "&FunctionID=" + FunctionID+"';");
            //    }
            //    else
            //    {
            //        ScriptHelper.Window.Alert("保存失败！");
            //    }
            //}
            //catch (BaseException bex)
            //{
            //    ExceptionHelper.ExceptionDistribute(bex, false);
            //    ScriptHelper.Window.Alert(bex.Message);
            //}
            //catch (Exception ex)
            //{
            //    ExceptionHelper.ExceptionDistribute(ex, false);
            //    ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            //}
        }

        /// <summary>
        /// 修改
        /// </summary>
        private void Edit()
        {
            //try
            //{
            //    Bu_AuditBatch obj = GetSubmitObject();
            //    bool ret = new Honestysoft.Financing.BusinessLogic.Audit(Globals.CurrentConnStringName).UpdateBatch(obj);
            //    if (ret)
            //    {
            //        ScriptHelper.Window.Alert("保存成功！", "window.location.href='Bu_AuditBatch_View.aspx?ID=" + obj.ID + "&FunctionID=" + FunctionID + "';");
            //    }
            //    else
            //    {
            //        ScriptHelper.Window.Alert("保存失败！");
            //    }
            //}
            //catch (BaseException bex)
            //{
            //    ExceptionHelper.ExceptionDistribute(bex, false);
            //    ScriptHelper.Window.Alert(bex.Message);
            //}
            //catch (Exception ex)
            //{
            //    ExceptionHelper.ExceptionDistribute(ex, false);
            //    ScriptHelper.Window.Alert("保存失败！系统发生了未知错误！");
            //}
        }
        /// <summary>
        /// 得到提交对象
        /// </summary>
        /// <returns></returns>
        private Bu_AuditBatch GetSubmitObject()
        {
            Bu_AuditBatch obj = new Bu_AuditBatch();
            if (!string.IsNullOrEmpty(Request.QueryString["ID"]))
            {
                obj.ID = Request.QueryString["ID"];
            }
            else
            {
                obj.ID =  Honestysoft.BaseLibrary.Common.Guid.GetGuid();
            }
            //obj.ID = txtID.Text;
            if (numBatchNo.Text.Trim().Length > 0) { obj.BatchNo = Convert.ToInt32(numBatchNo.Text); }
            if (dtpPStartDate.Text.Trim().Length > 0) { obj.PStartDate = Convert.ToDateTime(dtpPStartDate.Text); }
            if (dtpPEndDate.Text.Trim().Length > 0) { obj.PEndDate = Convert.ToDateTime(dtpPEndDate.Text); }
            if (dtpStartDate.Text.Trim().Length > 0) { obj.StartDate = Convert.ToDateTime(dtpStartDate.Text); }
            if (dtpEndDate.Text.Trim().Length > 0) { obj.EndDate = Convert.ToDateTime(dtpEndDate.Text); }
            obj.Remark = txtRemark.Text;
            obj.Creator = hidCreator.Value;// txtCreator.Text;
            //obj.CloseUser = txtCloseUser.Text;
            if (dtpCreateTime.Text.Trim().Length > 0) { obj.CreateTime = Convert.ToDateTime(dtpCreateTime.Text); }
            obj.BatchStatus = ddlBatchStatus.SelectedValue;

            obj.BatchContracts = new List<Bu_BatchContract>();
            obj.BatchRecs = new List<Bu_BatchRec>();
            if (hidContractIDString.Value.Trim().Length > 0)
            {
                Bu_BatchContract item = null;
                string[] arrId = hidContractIDString.Value.Trim().Split(',');
                foreach (string s in arrId)
                {
                    item = new Bu_BatchContract();
                    item.BatchID = obj.ID;
                    item.ContractID = s;
                    item.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    obj.BatchContracts.Add(item);
                }
            }

            if (hidRecIDString.Value.Trim().Length > 0)
            {
                Bu_BatchRec item = null;
                string[] arrId = hidRecIDString.Value.Trim().Split(',');
                foreach (string s in arrId)
                {
                    item = new Bu_BatchRec();
                    item.BatchID = obj.ID;
                    item.RecID = s;
                    item.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    obj.BatchRecs.Add(item);
                }
            }

            return obj;
        }

        protected void btnReBind_Click(object sender, EventArgs e)
        {
            BindContract();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            BindContract();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            BindContract();
        }
        /// <summary>
        /// 得到选中的记录ID集合
        /// </summary>
        /// <returns></returns>
        private List<string> GetSelectedRowKeys()
        {
            string strId = Request["chkID"] + "";
            if (string.IsNullOrEmpty(strId)) { return null; }
            List<string> lsRet = new List<string>();
            lsRet.AddRange(strId.Split(','));
            return lsRet;
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            List<string> lsDelId = GetSelectedRowKeys();
            string strConID = hidContractIDString.Value;
            string[] arrConID = strConID.Split(',');
            string strNewConID = "";
            for (int i = 0; i < arrConID.Length; i++)
            {
                if (!lsDelId.Contains(arrConID[i]))
                {
                    if (strNewConID != "")
                    {
                        strNewConID += ",";
                    }
                    strNewConID += arrConID[i];
                }
            }
            hidContractIDString.Value = strNewConID;
            BindContract();
        }
    }
}